package com.mstar.qs.common.jmx;

import java.util.HashSet;
import java.util.Set;

public class DefExpose implements JmxExpose {

	public DefExpose() {

	}

	private Set<String> includingAttributes;
	private Set<String> includingOperations;

	@Override
	public void addAttribute(String... attribute) {
		if (includingAttributes == null)
			includingAttributes = new HashSet<String>();
		for (int i = 0; i < attribute.length; i++) {
			includingAttributes.add(attribute[i]);
		}
	}

	@Override
	public void addOperation(String... operation) {
		if (includingOperations == null)
			includingOperations = new HashSet<String>();
		for (int i = 0; i < operation.length; i++) {
			includingOperations.add(operation[i]);
		}
	}

	@Override
	public void removeAttribute(String... attribute) {
		if (includingAttributes != null
				&& includingAttributes.contains(attribute))
			for (int i = 0; i < attribute.length; i++) {
				includingAttributes.remove(attribute[i]);
			}

	}

	@Override
	public void removeOperation(String... operation) {
		if (includingOperations != null
				&& includingOperations.contains(operation))
			for (int i = 0; i < operation.length; i++) {
				includingOperations.remove(operation[i]);
			}
	}

	@Override
	public String[] obtainAttributes() {

		if (includingAttributes != null) {
			String[] att = new String[includingAttributes.size()];
			return (String[]) includingAttributes.toArray(att);
		}
		return null;
	}

	@Override
	public String[] obtainOperations() {

		if (includingOperations != null) {
			String[] ops = new String[includingOperations.size()];
			return (String[]) includingOperations.toArray(ops);
		}
		return null;
	}

	@Override
	public void registerMBean(String domain, String type, String name) {
		MBeanAgent agent = new MBeanAgent(this, obtainOperations(),
				obtainAttributes());
		agent.setObjectNameStr(domain, type, name);
		MBManager.getInstance().putMBean(this, agent);
	}

	@Override
	public void unregisterMBean(Object o) {
		MBManager.getInstance().unregisterMBean(o);

	}

	@Override
	public void unregisterMBean() {
		MBManager.getInstance().unregisterMBean(this);

	}

}
